package cn.ac.iie.di.tools;

import cn.ac.iie.di.global.GlobalParas;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class MppJdbcPool_dbcp {
    
    private static List<Connection> sessionList = new ArrayList<Connection>();
//    private static int MPPDBSIZE = 3;
//    private static String MPPDBUSER = "";
//    private static String MPPDBPASSWD = "";
//    private static String MPPDBURL = "";
//    private static String MPPDBDRIVER = "";
    static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(MppJdbcPool_dbcp.class.getName());
    
    static {
        try {

//            Properties propget = new Properties();
//            try {
////                String relativelyPath = System.getProperty("user.dir");
////                String filePath = relativelyPath + "/cts_env.properties";
//                String filePath = ClassLoader.getSystemClassLoader().getResource("cts_env.properties").getPath();
//                FileInputStream in = new FileInputStream(filePath);
//                propget.load(in);
//                in.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            //�������ļ��ж�ȡ
            // DBUSER = "sysdba";
//            MPPDBUSER = LoadProperties1("MPPDBUSER");
//            // DBPASSWD = "szoscar55";
//            MPPDBPASSWD = LoadProperties1("MPPDBPASSWD");
//            // DBURL = "jdbc:oscar://localhost:2003/osrdb";
//            MPPDBURL = LoadProperties1("MPPDBURL");
//            // DBDRIVER = "com.oscar.Driver";
//            MPPDBDRIVER = LoadProperties1("MPPDBDRIVER");
////            logger.info("6666666666666xxx" + LoadProperties1("MPPDBSIZE"));
//            MPPDBSIZE = Integer.valueOf(LoadProperties1("MPPDBSIZE"));
            Class.forName(GlobalParas.MPPDBDRIVER);
        } catch (Throwable e) {
            logger.error("MPP JdbcPool ERROR :" + e, e);
            throw new ExceptionInInitializerError(e);
        }
    }
    
    public static Connection getConnection() throws SQLException {
        Connection s = null;
        
        synchronized (sessionList) {
            if (sessionList.size() > 0) {
                s = sessionList.remove(0);
            }
        }
        if (s == null) {
            logger.info("get Connection start");
            DriverManager.setLoginTimeout(5);
            s = DriverManager.getConnection(GlobalParas.MPPDBURL, GlobalParas.MPPDBUSER, GlobalParas.MPPDBPASSWD);
            logger.info("get Connection end");
        }
        return s;
    }
    
    public static void closeConn(Connection session) {
        synchronized (sessionList) {
            try {
                if (sessionList.size() >= GlobalParas.MPPDBSIZE) {
                    session.close();
                } else {
                    sessionList.add(session);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void removeConn(Connection session) {
        synchronized (sessionList) {
            try {
                sessionList.remove(session);
                session.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
